package com.orangelife.mobile.myhouse.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.curry.android.http.OrangeHttpBiz;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.orangelife.mobile.R;
import com.datatrack.DataTrack;
import com.markupartist.android.widget.PullToRefreshListView;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.common.activity.PullRefreshBaseActivity3;
import com.common.jump.HouseJumper;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.myhouse.adapter.HouseLvAdapter;
import com.orangelife.mobile.myhouse.biz.HouseBiz;
import com.orangelife.mobile.myhouse.biz.HouseHttpBiz;
import com.orangelife.mobile.myhouse.entity.HouseBean;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 房屋列表
 * Created by 吴格非 on 2015/11/10.
 */
public class HouseListActivity extends PullRefreshBaseActivity3 {

    @Bind(R.id.plvList)
    PullToRefreshListView plvList;
    @Bind(R.id.includeWaitConfirmBar)
    View includeWaitConfirmBar;
    @Bind(R.id.tvJumpWaitConfirm)
    TextView tvJumpWaitConfirm;
    @Bind(R.id.ivClose)
    ImageView ivClose;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_right)
    ImageView ivTitleRight;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    private HouseLvAdapter adapter;
    private List<HouseBean> mList = new ArrayList<>();
    private boolean isFirstEnter = true;
    /**Activit 名称**/
    final  String MODULE_NAME="/mine/perInfo/myHouse";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_list);
        ButterKnife.bind(this);
        requestList();
        setAdapter();
        setListener();
        initTitle();
        setXListViewAndEmptyLayoutListener(plvList, emptyLayout, adapter);
        DataTrack.browseTrack(DataTrack.APP_NAME,MODULE_NAME,null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestWaitConfirmCount();
        if (!isFirstEnter) {
            requestList();
        }
        isFirstEnter = false;
    }

    /**
     * 数据采集体不要删除
     * @return
     */
    public Map<String,Object> getDataTrack(){
        Map<String,Object> map=new HashMap<>();
        map.put("UI_HOST", DataTrack.APP_NAME);
        map.put("UI_PATH", MODULE_NAME);
        return map;
    }

    private void jumpToEmpty() {
        HouseJumper.jumpToTip(this, HouseTipActivity.TYPE_TIP_UNCERT, null, null, null);
        CurryApplication.getInstance().finishActivity(this);
    }


    private void setAdapter() {
        adapter = new HouseLvAdapter(this);
    }

    @OnClick({R.id.tvJumpWaitConfirm, R.id.ivClose, R.id.iv_title_right})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvJumpWaitConfirm:
                HouseJumper.switchJump(HouseListActivity.this, v);
                break;
            case R.id.ivClose:
                includeWaitConfirmBar.setVisibility(View.GONE);
                break;
            case R.id.iv_title_right:
                HouseJumper.jumpToCert(HouseListActivity.this, "false");
                break;
        }
    }

    private void initTitle() {
        tvTitle.setText("我的房屋");
        ivTitleRight.setVisibility(View.VISIBLE);
        ivTitleRight.setBackgroundResource(R.drawable.ic_new_add);
    }

    private void setListener() {
        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestList();
            }
        });
    }

    /** 请求待认证条数 */
    private void requestWaitConfirmCount() {
        HouseHttpBiz.requestWaitConfirmCount(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                HashMap<String, String> map = HouseHttpBiz.handleWaitConfirmCount(response);
                HouseBiz.handleConfirmWaitBar(includeWaitConfirmBar, tvJumpWaitConfirm, map);
            }
        });
    }

    /** 请求房屋列表 */
    private void requestList() {
        HouseHttpBiz.requestHouseList(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                dismissProgress();
                if (response != null) {
                    List<HouseBean> listLoaded = HouseHttpBiz.handleHouseList(response);
                    dismissProgress();
                    handleAfterRequest(isConnected(), OrangeHttpBiz.getTotalNum(response), listLoaded, plvList, adapter);
                }
                if (mList != null && pageNo == 1 && mList.size() == 0) {
                    jumpToEmpty();
                }
            }
        }, pageSize, pageNo);
    }

    @Override
    protected boolean isDelayRequest() {
        return true;
    }

    @Override
    protected EmptyLayout getEmptyLayout() {
        return emptyLayout;
    }

    @Override
    protected List getData() {
        return mList;
    }

    @Override
    protected int getDataCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    protected void requestWhenPullRefresh() {
        requestList();
    }

}
